﻿Imports System.Configuration.ConfigurationManager
Imports System.Data
Imports System.Data.SqlClient

Partial Class ConsultasMenu
    Inherits System.Web.UI.Page

    Dim cn As New SqlConnection(ConnectionStrings("cn").ConnectionString)
    Dim consulta As New ConsultaIntranet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            CargarModelosCel()
            procesarQuery()
        End If

    End Sub

    Sub CargarModelosCel()
        Dim StrQuery As String = "select   eq.cod_equipo cod,eq.Equip_desc 'desc',SUM(de.cantidad) " & _
                                 " from dbo.tb_detalle_orden_compra de ,dbo.tb_equipo eq " & _
                                 " where de.cod_equipo = eq.cod_equipo " & _
                                 " group by eq.cod_equipo,eq.Equip_desc " & _
                                 " order by SUM(de.cantidad) "

        Dim dt As DataTable = consulta.ListadoParametro(StrQuery)
        CbModelo.DataSource = dt
        CbModelo.DataTextField = "desc"
        CbModelo.DataValueField = "cod"
        CbModelo.DataBind()

        CbModelo.Items.Insert(0, "--Todos--")

    End Sub

    Protected Sub btnProcesar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnProcesar.Click

        procesarQuery()

    End Sub

    Sub procesarQuery()
        If CbModelo.SelectedValue = "--Todos--" Then

            Dim StrQuery As String = "select  eq.Equip_desc 'Tipo',SUM(de.cantidad) cantidad " & _
                              " from dbo.tb_detalle_orden_compra de ,dbo.tb_equipo eq " & _
                              " where de.cod_equipo = eq.cod_equipo " & _
                              " group by eq.Equip_desc " & _
                              " order by SUM(de.cantidad) "

            Dim dt As DataTable = consulta.ListadoParametro(StrQuery)
            GridView1.DataSource = dt
            GridView1.DataBind()

        Else

            Dim codigo As String = CbModelo.SelectedValue
            Dim StrQuery As String = "select  eq.Equip_desc 'Tipo',SUM(de.cantidad) cantidad " & _
                              " from dbo.tb_detalle_orden_compra de ,dbo.tb_equipo eq " & _
                              " where de.cod_equipo = eq.cod_equipo and " & _
                              " de.cod_equipo = '" & codigo & "' " & _
                              " group by eq.Equip_desc " & _
                              " order by SUM(de.cantidad) "

            Dim dt As DataTable = consulta.ListadoParametro(StrQuery)
            GridView1.DataSource = dt
            GridView1.DataBind()

        End If
    End Sub
End Class
